#!  /usr/bin/env pytreon
#ecoding=utf
from ..utils.db_utils import DBUtils
from pymysql import cursors
import traceback


class TemplateDao():
    @staticmethod
    def getOneByRequestId(request_id):
        try:
            # 检测当前正在运行连接数的是否小于最大链接数，如果不小于则：等待或报raise TooManyConnections异常
            # 否则
            # 则优先去初始化时创建的链接中获取链接 SteadyDBConnection。
            # 然后将SteadyDBConnection对象封装到PooledDedicatedDBConnection中并返回。
            # 如果最开始创建的链接没有链接，则去创建一个SteadyDBConnection对象，再封装到PooledDedicatedDBConnection中并返回。
            # 一旦关闭链接后，连接就返回到连接池让后续线程继续使用。
            conn = DBUtils.get_connection_pool().connection()
            sql = 'select a.* from t_template a where a.request_id =%s'
            cursor=conn.cursor(cursor=cursors.DictCursor)
            cursor.execute(sql,(request_id,))
            rss = cursor.fetchone()
            return rss
        except Exception as e:
            print('error where execute fetch....rollback...'+str(request_id))
            traceback.print_exc()

    # @staticmethod
    # def update(obj):
    #     try:
    #         cursor = conn.cursor(cursor=cursors.DictCursor)
    #         sql = "update t_template_url  set status = 5 , send_status=2 where id ="+str(obj['id'])
    #         cursor.execute(sql)
    #     except Exception as e:
    #         conn.rollback()
    #         print('error where execute update.....rollback..')
    #         traceback.print_exc()
    #     finally:
    #         conn.commit()